package it.mauriziobasile.dietarapida.gui.tablemodels;

import it.mauriziobasile.dietarapida.gui.DietaRapidaView;

import java.text.DecimalFormat;
import java.util.Hashtable;
import java.util.Properties;

import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Maurizio Basile
 */
public class AlimRiassuntoTableModel extends DefaultTableModel{

    private static final long serialVersionUID = 6489683470696009479L;

    private DecimalFormat df=new DecimalFormat("#.##");

     private DietaRapidaView view;
         
     private Hashtable<String,Double> kcalFactor;


    public AlimRiassuntoTableModel(DietaRapidaView view) {
        this.view=view;
        kcalFactor= new Hashtable<String, Double>();
        kcalFactor.put("PROTEINE", 4.0);
        kcalFactor.put("LIPIDI", 9.0);
        kcalFactor.put("CARBOIDRATI", 3.75);
        kcalFactor.put("ALCOOL", 7.0);
        Properties pp = view.getProperties();
        try {
            String ntr = pp.getProperty("nutriRiassunto");
            String[] nutrienti = ntr.split(";");
            Object[] nullv = new Object[nutrienti.length + 1];
            addColumn("Tot."); //SEMPRE PRIMA COLONNA
            //addColumn("Quantità");  //SEMPRE SECONDA COLONNA
            for (int i = 0; i < nutrienti.length; i++) {
                addColumn(nutrienti[i]);
            }
            nullv[0]="Grammi";
            addRow(nullv);
            nullv[0]="KCal %";
            addRow(nullv);
        } catch (Exception e) {
        }
        update();
    }
        @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public void update(){
        double tmp=0.0;
        int cc=getColumnCount();
        double kcaltot=view.getDieta().getTotal("ENER_KCAL")/100;
        for (int i=1;i<cc;i++){
            String nname=getColumnName(i);
            tmp=view.getDieta().getTotal(nname);
            setValueAt(df.format(tmp), 0, i);
            if (kcaltot > 0.0){
                if (kcalFactor.containsKey(nname)){
                    setValueAt(df.format(kcalFactor.get(nname)*tmp/kcaltot),1,i);
                }
            }
            else{
                setValueAt("-",1,i);
            }
                
        }
    }
}
